$(function () {
    $("#loginOrganizationDIV #loginButton").click(loginOrganization);
    $("#regOrganizationDIV #regButton").click(regOrganization);
})

function loginOrganization() {
    var organizationId = $("#loginOrganizationDIV :text[name='organizationId']").val();
    var password = $("#loginOrganizationDIV :password[name='password']").val();
    var remember = $("#loginOrganizationDIV :checkbox[name='remember']:checked").val();
    if (remember == undefined) {
        remember = "no";
    }

    $.post("/login/loginOrganization?t=" + new Date().getTime(), {
        "organizationId": organizationId,
        "password": password,
        "remember": remember,
    }, function (data) {
        var jsonObject = JSON.parse(data);
        if (jsonObject.responseCode == 500) {
            layer.msg(jsonObject.message);
        }
        if (jsonObject.responseCode == 501) {
            layer.msg("系统级异常：" + jsonObject.message);
        }
        if (jsonObject.responseCode == 200) {
            if (jsonObject.responseData == true) {
                location.href = "../views/index.html";
            } else {
                layer.msg('机构ID或密码不正确，登陆失败，请重新登陆！');
            }
        }
    });
}

function regOrganization() {
    var organizationName = $("#regOrganizationDIV :text[name='organizationName']").val();
    var contactsName = $("#regOrganizationDIV :text[name='contactsName']").val();
    var contactsSex = $("#regOrganizationDIV select[name='contactsSex'] option:selected").val();
    var organizationId = $("#regOrganizationDIV :text[name='organizationId']").val();
    var password = $("#regOrganizationDIV :password[name='password']").val();
    var email = $("#regOrganizationDIV :text[name='email']").val();
    var phone = $("#regOrganizationDIV :text[name='phone']").val();

    $.post("/reg/regOrganization?t=" + new Date().getTime(), {
        "organizationName": organizationName,
        "contactsName": contactsName,
        "contactsSex": contactsSex,
        "organizationId": organizationId,
        "password": password,
        "email": email,
        "phone": phone,
    }, function (data) {
        var jsonObject = JSON.parse(data);
        if (jsonObject.responseCode == 500) {
            layer.msg(jsonObject.message);
        }
        if (jsonObject.responseCode == 501) {
            layer.msg("系统级异常：" + jsonObject.message);
        }
        if (jsonObject.responseCode == 200) {
            $("#loginOrganizationDIV :text[name='organizationId']").val($("#regOrganizationDIV :text[name='organizationId']").val());
            $("#regOrganizationDIV :text[name='organizationName']").val("");
            $("#regOrganizationDIV :text[name='contactsName']").val("");
            $("#regOrganizationDIV select[name='contactsSex']").val("男");
            $("#regOrganizationDIV :text[name='organizationId']").val("");
            $("#regOrganizationDIV :password[name='password']").val("");
            $("#regOrganizationDIV :text[name='email']").val("");
            $("#regOrganizationDIV :text[name='phone']").val("");

            layui.use('form', function () {
                var form = layui.form;
                form.render();
            });

            layer.msg('注册成功，请等待审批通过！');
        }
    });
}